<?php
namespace Operation;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

use Operation\Model\StoreTable;
use Operation\Model\SaleheadTable;
use Operation\Model\SalerowTable;
use Operation\Model\MeteoTable;
use Operation\Model\MeteotypeTable;
use Operation\Model\StorecountTable;
use Operation\Model\DrawercountTable;

class Module
{ 
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
        
        $sharedEvents = $eventManager->getSharedManager();
        $sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
            $controller = $e->getTarget();
            $route = $controller->getEvent()->getRouteMatch();
            $e->getViewModel()->setVariables(
                array('controllerName'=> $route->getParam('__CONTROLLER__', 'index'),
                    'actionName' => $route->getParam('action', 'index'),
                    'moduleName' => strtolower(__NAMESPACE__))
            );
        }, 100);
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }
    
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__
                ),
            ),
        );
    }
    
    public function getServiceConfig()
    {
        return array(
            'factories' => array(    
                // Service for Store table
                'Operation\Model\StoreTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new StoreTable($dbAdapter);
                    return $table;
                }, 
                        
                // Service for Salehead table
                'Operation\Model\SaleheadTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new SaleheadTable($dbAdapter);
                    return $table;
                }, 
                        
                // Service for Salerow table
                'Operation\Model\SalerowTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new SalerowTable($dbAdapter);
                    return $table;
                }, 
                        
                // Service for Meteo table
                'Operation\Model\MeteoTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new MeteoTable($dbAdapter);
                    return $table;
                }, 
                        
                // Service for Meteotype table
                'Operation\Model\MeteotypeTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new MeteotypeTable($dbAdapter);
                    return $table;
                }, 
                        
                // Service for Storecount table
                'Operation\Model\StorecountTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new StorecountTable($dbAdapter);
                    return $table;
                }, 
                        
                // Service for Drawercount table
                'Operation\Model\DrawercountTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new DrawercountTable($dbAdapter);
                    return $table;
                }, 
            ),
        );
    }
}
